﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;

/// <summary>
/// SettingsGradeTemplate 的摘要说明
/// </summary>
public class SettingsGradeTemplate : ITemplate, IRequiresSessionState
{
    private const int VIEWCOUNTS = 5;
    private static int _itemCount = -1;
    private readonly ListItemType _templateType;
    private IList<EvalItems> _evalItemList;
    private IList<EvalSettingsExtend> _settingsList;
    private IGlobalCauseInfoLogic _globalCauseInfoLogic = Container<IGlobalCauseInfoLogic>.Instance;

    public SettingsGradeTemplate(ListItemType type, IList<EvalSettingsExtend> settingsList, IList<EvalItems> evalItemList)
    {
        _templateType = type;
        _settingsList = settingsList;
        _evalItemList = evalItemList;
    }

    public void InstantiateIn(Control container)
    {
        try
        {
            if (_settingsList == null) return;
            Literal lc = new Literal();
            switch (_templateType)
            {
                case ListItemType.Header:
                    _itemCount = -1;
                    lc.Text = GeneratorHead();
                    break;
                case ListItemType.Item:
                    lc.Text = GeneratorItem();
                    break;
                case ListItemType.AlternatingItem:
                    lc.Text = GeneratorItem();
                    break;
                case ListItemType.Footer:
                    lc.Text = "</TABLE>";
                    break;
            }
            container.Controls.Add(lc);
            _itemCount += 1;
        }
        catch (Exception ex)
        {
            return;
        }
    }

    /// <summary>
    /// 生成头部文件
    /// </summary>
    /// <returns></returns>
    private string GeneratorHead()
    {
        string header = @"
            <table style='width: 98%; margin: auto;' class='useList'>
                <tr>
                    <td style='width: 6%; background-color: #D4F1F9;'>
                        工号</td>
                    <td style='width: 6%; background-color: #D4F1F9;'>
                        教师</td>
                    <td style='width: 240px; background-color: #D4F1F9;'>
                        年级组范围</td>
                    {0}
                    <td style='width: 60px; background-color: #D4F1F9;'>
                    </td>
                </tr>";
        string append = "";
        foreach (EvalItems item in _evalItemList)
        {
            append += "<td style='width: 120px; background-color: #D4F1F9;'>" + item.EvalName + "</td>";
        }
        header = string.Format(header, append);

        return header;
    }

    /// <summary>
    /// 生成主体部分
    /// </summary>
    /// <returns></returns>
    private string GeneratorItem()
    {
        if (_itemCount >= _settingsList.Count) return "";
        string body = @"
            <tr>
                <td style='text-align: left; padding-left: 5px;'>
                    {0}
                </td>
                <td style='text-align: left; padding-left: 5px;'>
                    {1}
                </td>
                <td style='text-align: left;'>
                    &nbsp;&nbsp;{2}
                </td>
                {3}
                <td>
                    <img alt='修改' src='../Common/images/edit.jpg' style='cursor:hand' onclick='modifyEvalSetting({4})' />
                    <img alt='删除' src='../Common/images/delete.jpg' style='cursor:hand' onclick='deleteEvalSetting({4})' />
                </td>
            </tr>";

        EvalSettingsExtend settings = _settingsList[_itemCount];
        StringBuilder sb = new StringBuilder();
        foreach (EvalItems item in _evalItemList)
        {
            sb.Append("<td>");
            bool rule = ExistsThisRule(settings.TaskIDList, item.RID);
            if (rule) sb.Append("<img alt=\"有权限\" src=\"../Common/images/openExam1.gif\" />");
            sb.Append("</td>");
        }
        string td = sb.ToString();

        string gradeStr;
        if (!string.IsNullOrEmpty(ContainerBasePage.SchoolMark))
        {
            IList<int> gradeList = new List<int>();
            string[] idList = settings.GradeList.Split(',');
            foreach (string s in idList)
            {
                int id;
                bool flag = int.TryParse(s, out id);
                if (flag) gradeList.Add(id);
            }
            string[] results = ViewGradeList(gradeList);
            gradeStr = "<span {0}>{1}</span>";
            gradeStr = string.Format(gradeStr, results[1] == "" ? "" : "title='" + results[1] + "'",
                                             results[0]);
        }
        else
        {
            gradeStr = "";
        }


        body = string.Format(body, settings.TeachCode, settings.TeachName, gradeStr, td, settings.RID);
        return body;
    }

    /// <summary>
    /// 判断权限
    /// </summary>
    /// <param name="taskIdList"></param>
    /// <param name="taskID"></param>
    /// <returns></returns>
    protected bool ExistsThisRule(string taskIdList, int taskID)
    {
        string[] idList = taskIdList.Split(',');
        foreach (string id in idList)
        {
            if (id == "") continue;
            if (taskID == int.Parse(id))
                return true;
        }

        return false;
    }
    /// <summary>
    /// 显示教研组范围
    /// </summary>
    /// <param name="causeList"></param>
    private static string[] ViewGradeList(IList<int> gradeList)
    {
        if (gradeList == null || gradeList.Count <= 0) return null;
        string viewTaskName = "";
        string promptTaskName = "";
        int count = 0;
        foreach (int gradeIndex in gradeList)
        {
            Grade grade = Grade.ConvertGrade(gradeIndex);
            if (count < VIEWCOUNTS)
            {
                viewTaskName += grade.GradeName + ",";
            }
            promptTaskName += grade.GradeName + ",";

            count++;
        }
        viewTaskName = viewTaskName.TrimEnd(',');
        promptTaskName = promptTaskName.TrimEnd(',');
        return new string[] { viewTaskName, promptTaskName };
    }
}
